package gui;

import flexibility.showMessage;
import flexibility.uploadFileInfo;
import guiCode.checkDataBase;
import guiCode.studentScreenCode;
import guiCode.uploadManagerCode;

import javax.swing.JFrame;
import javax.swing.SpringLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JTextField;

import basic.ClientConsole;
import java.awt.Rectangle;
import java.awt.Color;

/**
 * This class handles the upload manager GUI
 */
public class uploadManager extends JFrame {

	private JTextField textField;
	static uploadManagerCode upload_man_code;
	private ClientConsole start_connection;
	private File file;
	private Integer userID;
	private showMessage msg = new showMessage();
	public static Integer UM_fileID = 0;
	public uploadFileInfo fileInfo;

	/**
	 * The constructor of the class
	 * 
	 * @param str_conn
	 *            - connection to server
	 * @param user_ID
	 *            - user's ID
	 * @param fileInfo
	 *            - object with all the necessary information to upload the file
	 */
	public uploadManager(ClientConsole str_conn, Integer user_ID,
			uploadFileInfo fileInfo) {
		setBounds(new Rectangle(400, 200, 600, 200));
		start_connection = str_conn; // Connection to the server
		userID = user_ID;
		this.fileInfo = fileInfo;
		upload_man_code = new uploadManagerCode(start_connection,
				uploadManager.this);
		createUploadManager();
	}

	/**
	 * This method creates a new window to upload the file
	 */
	public void createUploadManager() {
		setTitle("Upload Manager");

		SpringLayout springLayout = new SpringLayout();
		getContentPane().setLayout(springLayout);

		// Select File Button
		JButton findButton = new JButton("Select File");
		springLayout.putConstraint(SpringLayout.NORTH, findButton, 23,
				SpringLayout.NORTH, getContentPane());
		findButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				file = upload_man_code.findFile(); // find file process
				if (file != null)
					textField.setText(file.getName()); // show the selected file
														// name
			}

		});
		getContentPane().add(findButton);

		textField = new JTextField();
		textField.setBackground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.SOUTH, textField, 0,
				SpringLayout.SOUTH, findButton);
		springLayout.putConstraint(SpringLayout.EAST, textField, -6,
				SpringLayout.WEST, findButton);
		textField.setEditable(false);
		springLayout.putConstraint(SpringLayout.NORTH, textField, 24,
				SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.WEST, textField, 23,
				SpringLayout.WEST, getContentPane());
		getContentPane().add(textField);
		textField.setColumns(10);

		// Upload File Button
		JButton uploadButton = new JButton("Upload File");
		springLayout.putConstraint(SpringLayout.NORTH, uploadButton, 23,
				SpringLayout.NORTH, getContentPane());
		springLayout.putConstraint(SpringLayout.EAST, findButton, -28,
				SpringLayout.WEST, uploadButton);
		springLayout.putConstraint(SpringLayout.EAST, uploadButton, -10,
				SpringLayout.EAST, getContentPane());
		getContentPane().add(uploadButton);
		uploadButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (file == null) {
					msg.showMessages(uploadManager.this,
							"You must select a file to upload", "Upload Error",
							1);
				} else { //In case the file to upload was selected

					upload_man_code.setUserID(userID);
					upload_man_code.uploadfile(file, fileInfo);
					//check if upload was successful
					System.out.println(upload_man_code.uploadSuccess);
			    	if (upload_man_code.uploadSuccess == true) {
						//UM_fileID = upload_man_code.getFileID();
						if (fileInfo.getIfUpdate()) {
							msg.showMessages(uploadManager.this,
									"The file was replaced !", "Upload status",
									2);
						} else {
							// If the file was uploaded, update the necessary
							// tables with fileID
							upload_man_code.updateTables(fileInfo);
							msg.showMessages(uploadManager.this,
									"The file was uploaded !", "Upload status",
									2);
						}
					} else {
						msg.showMessages(uploadManager.this,
								"Uploading the file - Failed", "Upload error",
								1);
					}
			
				}
			}
		});

		setVisible(true);

	}
}
